package be.kdg.bombermanunlimited.server.network;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class SettingsServer {

	private ServerSocketListener ssl;

	public SettingsServer(int port) {
		ssl = new ServerSocketListener(port);
		new Thread(ssl).start();
	}

	private class ServerSocketListener implements Runnable {
		private ServerSocket socket;
		private boolean stop;

		public ServerSocketListener(int port) {
			try {
				socket = new ServerSocket(port);
			} catch (IOException e) {
				e.printStackTrace();
			}

			stop = false;
		}

		@Override
		public void run() {
			while (!stop) {
				try {
					Socket client = socket.accept();
					
					new Thread(new InformationServer(client)).start();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private class InformationServer implements Runnable {
		private Socket sckt;

		public InformationServer(Socket socket) {
			this.sckt = socket;
		}

		@Override
		public void run() {
			try {
				sckt.getOutputStream().write(
						sckt.getInetAddress().getHostAddress().getBytes());
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			try {
				sckt.shutdownInput();
				sckt.shutdownOutput();
				sckt.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
